
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class Test{

    public static void main(String[] args) throws InterruptedException {
        long startTime;
        long endTime;
//        double[][] A;
//        double[][] B;
//        double[][] result;
//        double[][] serial_result;
        double[][] A = new double[1000][1000];
        double[][] B = new double[1000][1000];
        double[][] result = new double[1000][1000];
        double[][] serial_result = new double[1000][1000];
        for(int i = 0;i < A.length;i++){
            for(int j = 0;j < A[0].length;j++){
                int a = (int) (Math.random()*100);
                double b = a/10.0;
                A[i][j] = b;
            }
        }
        for(int i = 0;i < B.length;i++){
            for(int j = 0;j < B[0].length;j++){
                int a = (int) (Math.random()*100);
                double b = a/10.0;
                B[i][j] = b;
            }
        }
//        MatrixGenerator matrixGenerator = new MatrixGenerator();

//        List<double[][]> list = matrixGenerator.lists;
//        A = list.get(0);
//        B = list.get(1);
//        result = list.get(2);
//        serial_result = list.get(3);
        int threadNum = 1000;
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        int gap = A.length/threadNum;
        startTime = System.currentTimeMillis();
        System.out.println("矩阵大小——A：("+A.length+","+A[0].length+") B:("+B.length+","+B[0].length+")");
        System.out.println("线程数为："+threadNum);
        for(int i = 0;i < A.length;i++){
            for(int j = 0;j < B[0].length;j++){
                for(int k = 0;k < B.length;k++){
                    serial_result[i][j] += A[i][k]*B[k][j];
                }
            }
        }
        endTime = System.currentTimeMillis();
        System.out.println("串行计算的时间："+(endTime-startTime));

        System.out.println("串行计算的结果矩阵的最后一个值"+serial_result[A.length-1][B[0].length-1]);

        startTime = System.currentTimeMillis();
        for(int i = 0;i < threadNum;i++){
            Run run = new Run(A,B,i,gap,result,countDownLatch);
            Thread thread = new Thread(run);
            thread.start();
        }
        countDownLatch.await();
        endTime = System.currentTimeMillis();
        System.out.println("并行计算的时间"+(endTime-startTime));
        System.out.println("并行计算的结果矩阵的最后一个值"+result[A.length-1][B[0].length-1]);

    }


}

